﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using clf.weighing.entity;



namespace weighing
{
    public class RoleDao : BaseDao<Role>
    {
        #region instance

        private static RoleDao? instance;
        public static RoleDao Instance => instance ??= new RoleDao();

        public override string GetTableName()
        {
            return TableName.sys_role.ToString();
        }

        #endregion

        public int Insert(Role role)
        {
            if(role == null)return 0;
            string sql = SqlBuilder.GetInsertSql(role, GetTableName());
            return Sqlite.Insert(sql);
        }

        public int Update(Role role)
        {
            string sql = SqlBuilder.GetUpdateSql(role, GetTableName());
            return Sqlite.Update(sql);
        }

        /// <summary>
        /// 根据用户的Id，获取角色的名称
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <returns>多个角色，以 ，分开</returns>
        public string ConverterName(long userid)
        {
            List<Role> roles = GetUserRoles(userid);
            string res = ",";
            for (int i = 0; i < roles.Count; i++)
            {
                res += roles[i].name + ",";
            }
            return res.TrimStart(',').TrimEnd(',');
        }

        public List<Role> GetUserRoles(long userid)
        {
            string sql = $@"SELECT r.* from {TableName.sys_role} r 
                            LEFT JOIN {TableName.sys_user_role} ur on r.id = ur.role_id
                            LEFT JOIN `{TableName.user}` u on u.id = ur.user_id
                            where u.id ={userid} order by {RoleField.type} desc;";
            var data = Sqlite.Select<Role>(sql);
            return data;
        }

        public List<Role> AllList()
        {
            string condition = $"{RoleField.type} < {(int)UserRoleType.Developer}";
            string orderby = $"{RoleField.type} desc";
            string sql = SqlBuilder.GetSelectSql(GetTableName(), null, condition, null, null, orderby);
            var data = Sqlite.Select<Role>(sql);
            return data;
        }

        public int Delete(Role role)
        {
            if (role == null) return 0;
            string sql = SqlBuilder.GetDeleteSql(GetTableName(), $"{RoleField.id} = {role.id}");
            return Sqlite.Delete(sql);
        }

        public bool Exist(string name)
        {
            string condition = $"{RoleField.name} = '{name}'";
            string sql = SqlBuilder.GetSelectSql(GetTableName(), "id", condition);
            var data = Sqlite.Select<Role>(sql);
            return data.Any();
        }

        /// <summary>
        /// 获取所有，排除超级管理员
        /// </summary>
        /// <returns></returns>
        public List<Role> List()
        {
            string condition ="";
            if (App.CurrentUser.roleType == (int)UserRoleType.Weighinger) return new List<Role>();
            if (App.CurrentUser.roleType == (int)UserRoleType.WeighingManager)
            {
                //排除超级管理员
                condition = $"{RoleField.type} < {(int)UserRoleType.Developer}";
            }
            string sql = SqlBuilder.GetSelectSql(GetTableName(), null, condition);
            var data = Sqlite.Select<Role>(sql);
            return data;
        }
    }
}
